#!/bin/bash
#description  binary install mysql 
SOFTDIR="/usr/local/src"
DATADIR="/data/mysql"
USER="mysql"
#create mysql user
if ! id $USER &>/dev/null ;then
      groupadd -g 3306 mysql
      useradd -u 3306 -g mysql -s /sbin/nologin -r mysql
fi
#create mysql directory
if [ ! -d $DATADIR ];then
      mkdir -p $DATADIR
fi
#Install mysql prerequisites
yum -y install libaio numactl bison-devel ncurses-devel libxml-devel 
yum remove -y mysql-libs
#install and configure mysql server
cd $SOFTDIR && wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.59-linux-glibc2.12-x86_64.tar.gz && tar xf mysql-5.5.59-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ && ln -sv /usr/local/mysql-5.5.59-linux-glibc2.12-x86_64  /usr/local/mysql && chown -R $USER:$USER /usr/local/mysql

cd /usr/local/mysql && scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=${DATADIR} --user=mysql
chown -R $USER.$USER $DATADIR
cp support-files/mysql.server /etc/init.d/mysqld
chmod u+x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysqld.sh
source /etc/profile.d/mysqld.sh

mv /etc/my.cnf{,.ori}
cat >/etc/my.cnf <<EOF
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
datadir=/data/mysql
innodb_log_file_size = 1G
log_error = error.log
skip_name_resolve = 1
EOF

#start mysql server

service mysqld start 

